package org.minuframework.utils;

import java.util.Date;
import java.util.Random;
import org.minuframework.lang.StringUtils;
import org.minuframework.time.TimeUtils;

/**
 * 获取随机字符串
 * 
 * @author 米mi
 */
public class RandomUtils {

    public static final String TIMESTAMP = "yyyyMMddHHmmssSSS";
    public static final String TIMESTAMP_UTC = "yyMMddHHmmssSSS";
    public static final String DATATIME = "yyyyMMddHHmmss";
    public static final String DATATIME_UTC = "yyMMddHHmmss";
    public static final String SIMPLETIME = "HHmmss";

    /**
     * 字符集
     */
    private static String[] paramArray = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B",
            "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
            "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};

    /**
     * 默认长度
     */
    private static int length = 16;

    /**
     * 获取指定长度的随机字符串
     * 
     * @param length 长度
     * @return 字符串
     */
    public static String getFK(int length) {
        StringBuffer sb = new StringBuffer();
        int len = length <= 0 ? RandomUtils.length : length;
        for (int i = 0; i < len; i++) {
            sb.append(getParam());
        }
        return sb.toString();
    }

    /**
     * 获取随机字符
     * 
     * @return 随机字符
     */
    private static String getParam() {
        Random ran = new Random();
        int next = ran.nextInt(paramArray.length - 1);
        String param = paramArray[next];
        return param;
    }

    /**
     * 打乱字符串
     * 
     * @param fk String 字符串
     * @return
     */
    public static String randomFK(String fk) {
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isEmpty(fk)) {
            return sb.toString();
        }

        char[] arr = fk.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            Random ran = new Random();
            int next = ran.nextInt(arr.length);
            sb.append(arr[next]);
        }

        return sb.toString();
    }

    /**
     * 获取随机数字
     * 
     * @param length 长度
     * @return
     */
    public static int getIntegerFK(int length) {
        int result = 0;
        if (length > 0 && length < 9) {
            Random ran = new Random();
            int key = getIntegerNext(length);
            result = ran.nextInt(key);
            while (getIntegerLength(result) < length) {
                result = ran.nextInt(key);
            }
        }
        return result;
    }

    /**
     * 随机生成6位数字
     * 
     * @return int
     */
    public static int randomUUId() {
        return getIntegerFK(6);
    }

    /**
     * 随机生成 param范围内的数字
     * 
     * @param param 数字范围
     * @return int（0 ~ param-1）
     */
    public static int randomUUId(int param) {
        if (param > 0) {
            Random ran = new Random();
            return ran.nextInt(param);
        } else {
            return 0;
        }
    }

    /**
     * 随机生成 param范围内的数字
     * 
     * @param param 数字范围
     * @param zero 是否允许0
     * @return int（1 ~ param-1）
     */
    public static int randomUUId(int param, boolean zero) {
        if (zero) {
            return randomUUId(param);
        }
        if (param > 1) {
            Random ran = new Random();
            int uuid = ran.nextInt(param);
            while (uuid <= 0) {
                uuid = ran.nextInt(param);
            }
            return uuid;
        } else {
            return 1;
        }
    }

    /**
     * 获取唯一id
     * 
     * @param format 日期格式
     * @param prefix 前缀
     * @param suffix 后缀
     * @param len 后缀长度
     * @return String
     */
    public static synchronized String randomUUId(String format, String prefix, int suffix,
            int len) {
        StringBuilder result = new StringBuilder();
        if (StringUtils.isNotEmpty(prefix)) {
            result.append(prefix);
        }
        result.append(TimeUtils.formatDateTime(new Date(), format));
        if (len > 0) {
            suffix++;
            if (suffix >= getIntegerNext(len)) {
                suffix = 0;
            }
            result.append(String.format("%0" + len + "d", suffix));
        }
        return result.toString();
    }

    private static int getIntegerNext(int length) {
        int result = 0;
        if (length > 0 && length < 9) {
            result = (int) (Math.pow(10, length) - 1);
        }
        return result;
    }

    private static int getIntegerLength(int key) {
        return String.valueOf(key).length();
    }

}
